package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.*;
import com.samehope.ar.service.IProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 问题 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    private IProblemService problemService;

    /**
     * 添加问题单
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody ProblemAddParam param) {
        problemService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 更新问题单
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody ProblemUpdateParam param) {
        problemService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除问题单
     * @param problemId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{problemId}")
    public CommonResult delete(@PathVariable("problemId") Long problemId) {
        problemService.deleteProblem(problemId);
        return CommonResult.success(null);
    }

    /**
     * 查询问题单详情
     * @param problemId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/detail/{problemId}")
    public CommonResult detail(@PathVariable("problemId") Long problemId) {
        return CommonResult.success(problemService.detail(problemId));
    }

    /**
     * 回复问题单
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/reply")
    public CommonResult reply(@Valid @RequestBody ReplyParam param) {
        problemService.reply(param);
        return CommonResult.success(null);
    }

    /**
     * 分配问题
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/allot")
    public CommonResult allot(@Valid @RequestBody ProblemAllotParam param) {
        problemService.allot(param);
        return CommonResult.success(null);
    }

    /**
     * 分页查询
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/problems")
    public CommonResult problems(@Valid PaginationParam paginationParam, ProblemQueryParam problemQueryParam) {
        return CommonResult.success(problemService.problems(paginationParam, problemQueryParam));
    }
}
